// pages/detailss/detailss.js
var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.globalData.imgurl
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDetail(options)
  },
  getDetail: function (e) {
    wx.showLoading({
      title: '正在加载',
      mask: true,
    })
    var that = this
    this.setData({
      deatilId: e.deatilId,
    })
    wx.request({
      url: app.globalData.url + 'getPartDeatilById?deatilId=' + e.deatilId,
      success: function (res) {
        that.setData({
          detail: res.data
        })
        var art = res.data.workcontent;
        WxParse.wxParse('art', 'html', art, that, 5);
      },
      complete: function () {
        setTimeout(function () {
          wx.hideLoading()
        }, 500)
      }
    })
  },
  // 报名
  signUp: function (e) {
    var that = this;
    wx.request({
      url: app.globalData.url + 'findByUserId?userId=' + wx.getStorageSync('userId'),
      header: {},
      success: function (res) {
        var personalinfo = res.data.body;
        console.log(personalinfo)
        // personalinfo.forEach(personalinfo)
        var emptyValues = that.toArray(personalinfo)
        console.log(emptyValues)
        if (emptyValues.length > 0) {
          wx.showModal({
            title: '提示',
            content: '您的个人信息不完整，请您先完善个人信息',
            showCancel: false,
            success: function (res) {
              wx.navigateTo({
                url: '/pages/message/message?',
              })
            },
            fail: function (res) { },
            complete: function (res) { },
          })

        } else {
          wx.request({
            url: app.globalData.url + 'toSignup?deatilId=' + that.data.deatilId + '&userId=' + wx.getStorageSync('userId'),
            success: function (res) {
              console.log(res)
              if (res.data.success) {
                wx.navigateTo({
                  url: '/pages/success/success',
                })
              } else if (res.data.msg == '用户不存在!') {
                wx.showModal({
                  title: '提示',
                  content: res.data.msg.substr(0, res.data.msg.length - 1) + ',请重新授权',
                  showCancel: true,
                  success: function (res) {
                    if (res.confirm) {
                      wx.navigateTo({
                        url: '/pages/login/login?signup=1',
                      })
                    } else {
                      wx.navigateBack({
                        delta: 1,
                      })
                    }
                  },
                })

              } else if (res.data.msg == '不能重复报名!') {
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                  showCancel: true,
                  success: function (res) {
                    wx.navigateBack({
                      delta: 1,
                    })
                  },
                })
              }
            }
          })
        }
        that.setData({
          personalinfo: personalinfo
        })
      },
      fail: function (res) {
        wx.navigateTo({
          url: '/pages/signup/message?',
        })
      },
      complete: function (res) { },
    })

  },
  // 对象转数组
  toArray: function (e) {
    var a = []
    var b = []
    for (var props in e) {
      a.push(props)
    }
    for (var i = 0; i < a.length; i++) {
      if (!e[a[i]] || e[a[i]].length == 0) {
        if (a[i] != 'remarks' && a[i] != 'currentStatus' && a[i] != 'integral'){
          b.push('{' + a[i] + ':' + e[a[i]] + '}')
        }

      }
    }
    return b
  },
  call:function(e){
    var o = e.currentTarget.dataset;
    wx.makePhoneCall({
      phoneNumber: o.mobile,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})